package com.ciaojian.worker.service;

import com.ciaojian.core.mapper.UserMapper;
import com.ciaojian.core.mapper.UserMemberMapper;
import com.ciaojian.core.model.User;
import com.ciaojian.core.model.UserMember;
import com.ciaojian.core.service.UserMemberService;
import com.ciaojian.core.service.UserService;
import com.ciaojian.worker.model.param.UserParam;
import com.ciaojian.worker.model.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @author Atlakyn
 */
@Service
public class UserServiceImpl extends UserService<UserMapper, User> {
    @Resource
    private UserMemberService<UserMemberMapper, UserMember> userMemberService;
    @Resource
    private OrderServiceImpl orderService;

    @Override
    public String getOpenIdById(Integer id) {
        return baseMapper.getOpenIdById(id);
    }

    /**
     * 查询用户
     *
     * @param userParam 参数
     * @return 用户信息
     */
    public UserVO getUser(UserParam userParam) {
        User user = this.lambdaQuery().eq(User::getTelphone, userParam.getTelephone()).one();
        Assert.notNull(user, "用户不存在");
        // 是否为VIP
        Integer isVip = userMemberService.checkUserMember(user.getId());
        Boolean isNew = orderService.countCompletedHairCutByUser(user.getId()) == 0;
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        userVO.setIsVip(isVip);
        userVO.setIsNew(isNew);
        return userVO;
    }
}